package org.yecq.tradedata.algorithm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 期货布林线
 *
 * @author yecq
 */
@Component
@Scope("prototype")
public class BollingerBandsFutures {

    private String code;
    private String date;

    @Value("${boll_ma_number:26}")
    private int ma_number;  // 移动平均线天数

    @Value("${boll_std_number:26}")
    private int std_number; //  标准差天数

    @Value("${boll_times:2}")
    private int times;      // 倍数

    @Autowired
    private MovingAverageFutures ma;

    BollingerBandsFutures() {
        this.code = null;
        this.date = null;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setMa_number(int ma_number) {
        this.ma_number = ma_number;
    }

    public void setStd_number(int std_number) {
        this.std_number = std_number;
    }

    public void setTimes(int times) {
        this.times = times;
    }

    public BollValue compute() {
        if (this.code == null || this.date == null) {
            throw new IllegalArgumentException("code和date为空");
        }
        try {
            BollValue value = new BollValue();
            // 求出mid，移动平均线
            this.ma.setCode(this.code);
            this.ma.setDate(this.date);
            this.ma.setDay_number(this.ma_number);
            double mid = this.ma.compute();

            // 计算标准差
            this.ma.setDay_number(this.std_number);
            double std = CommonAlgo.getStandardDeviation1(this.ma.getCloseArray());

            double top = mid + this.times * std;
            double bottom = mid - this.times * std;

            value.setMiddle(mid);
            value.setTop(top);
            value.setBottom(bottom);

            return value;
        } catch (NotEnoughKLineException ex) {
            return null;
        }
    }
}
